package com.siwei.mes.controller;


import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.entity.tgl.TglItemorder;
import com.siwei.mes.job.CleanSerialJob;
import com.siwei.mes.mapper.TglItemorderMapper;
import com.siwei.mes.tgl.init.TglProcessorInitializing;
import com.siwei.mes.tgl.queue.TglProcessorService;
import com.siwei.mes.tgl.service.SyncTglItemorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 测试接口
 *
 * @Description: 测试接口
 * @Version: 1.0
 * @author: chenmaolin
 * @date: 2024/1/6 22:22
 */

@Api(value = "测试接口")
@RestController
@RequestMapping("test")
public class TestController {
    @Resource
    private TglItemorderMapper tglItemorderMapper;
    @Resource
    private SyncTglItemorderService syncTglItemorderService;
    @Resource
    private TglProcessorService tglProcessorService;

    @Resource
    private CleanSerialJob cleanSerialJob;
    @Resource
    private TglProcessorInitializing tglProcessorInit;

    @AnonymousAccess
    @GetMapping("testQueue")
    public Json testQueue(@RequestParam("itemorderId") String itemorderId) {
        List<String> list = Arrays.asList(itemorderId.split(","));

        List<TglItemorder> itemorderList = tglItemorderMapper.selectByIds(list);
        for (TglItemorder tglItemorder : itemorderList) {
            tglProcessorInit.submitRequest(tglItemorder);
        }
        return Json.succ();
    }

    @AnonymousAccess
    @ApiOperation(value = "根据小票生成试验台账")
    @GetMapping("genExperiment")
    public Json genExperiment(@RequestParam("itemorderId") String itemorderId) {
        List<String> list = Arrays.asList(itemorderId.split(","));

        List<TglItemorder> itemorderList = tglItemorderMapper.selectByIds(list);
        for (TglItemorder tglItemorder : itemorderList) {
            tglProcessorInit.submitRequest(tglItemorder);
        }
        return Json.succ();
    }


    @AnonymousAccess
    @ApiOperation(value = "根据小票生成试验台账-快检的")
    @GetMapping("genExperimentByQuick")
    public Json genExperiment(@RequestParam("itid") Integer itid) {
        TglItemorder tglItemorder = tglItemorderMapper.selectByItId(itid);
        assertTrue(tglItemorder == null, "tgl小票信息不存在");
        /**这里判断快检 创建混凝土台账*/
        syncTglItemorderService.initQuickExperiment(tglItemorder);
        return Json.succ("根据小票生成试验台账-快检的");
    }

    @AnonymousAccess
    @ApiOperation(value = "根据小票生成试验台账-批检的")
    @GetMapping("genExperimentByBatch")
    public Json genExperimentByBatch(@RequestParam("itid") Integer itid) {
        TglItemorder tglItemorder = tglItemorderMapper.selectByItId(itid);
        assertTrue(tglItemorder == null, "tgl小票信息不存在");
        /**这里判断批检 创建混凝土台账*/
        syncTglItemorderService.initBatchExperiment(tglItemorder);
        return Json.succ("根据小票生成试验台账-批检的");
    }

    @AnonymousAccess
    @ApiModelProperty(value = "清空序号表")
    @GetMapping("cleanSerial")
    public Json cleanSerial() {
        cleanSerialJob.init();
        return Json.succ();
    }

    @AnonymousAccess
    @ApiModelProperty(value = "测试生产编号")
    @GetMapping("genCode")
    public Json genCode(@RequestParam("code") String code) {
        String s = UidUtils.genNo(code);
        return Json.succ().data(ApiResult.addColumn("code", s));
    }


}
